package com.zhss.dfs.namenode.server.context;

import com.zhss.dfs.common.PropertiesUtil;
import io.netty.util.internal.StringUtil;

import java.util.HashMap;
import java.util.Map;

public class DfsEnv implements Environment {
    private static DfsEnv INSTANCE = new DfsEnv();
    private Map<String, String> env = new HashMap<>();
    private final int DEFAULT_PORT = 50070;
    public static final Integer DEFAULT_EDIT_LOG_BUFFER_LIMIT = 25 * 1024;
    public static final String DEFAULT_EDIT_LOG_DIR_PATH = "/data/editlog";

    private DfsEnv() {
        init();
    }

    private void init() {
        env = PropertiesUtil.propertiesToMap("dfs-namenode.properties");
    }

    @Override
    public int getNettyPort() {
        String port = env.get("netty.port");
        if (port != null) {
            return Integer.parseInt(port);
        }
        return DEFAULT_PORT;
    }

    @Override
    public int getEditlogBufferLimit() {
        String limit = env.get("editlog.buffer.limit");
        if (limit != null) {
            return Integer.parseInt(limit);
        }
        return DEFAULT_EDIT_LOG_BUFFER_LIMIT;
    }

    @Override
    public String getEditlogDirPath() {
        String path = env.get("editlog.dir.path");
        if (!StringUtil.isNullOrEmpty(path)) {
            return path;
        }
        return DEFAULT_EDIT_LOG_DIR_PATH;
    }

    public static DfsEnv getInstance() {
        return INSTANCE;
    }
}
